package com.blog.dao;

import com.blog.bean.Blog;
import com.blog.bean.QueryCallback;
import com.blog.bean.User;
import com.blog.util.Util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {

    private final static String SELECT_BLOGS = "select * from blog " +
            "join user on blog.uid = user.id " +
            "left join thumbs_up t on blog.id = t.blog_id and t.uid = ";
    private final static String WHERE = " where blog.id < ";

    public void publishBlog(String content, int uid) throws SQLException {
        String sql = "insert into blog(uid, content, create_time) values(" +
                uid + ", '" +
                content + "', '" +
                Util.getNowDateString() + "')";
        System.out.println(sql);
        JdbcUtil.execute(sql);
    }

    /**
     * 查询最新微博
     *
     * @return
     */
    public List<Blog> selectLatestBlog(Integer id, int count, int uid) throws SQLException {
        String sql = SELECT_BLOGS + uid;
        if (id != null) {
            sql += WHERE + id;
        }
        sql += " order by blog.id desc limit " + count;
        System.out.println(sql);
        return (List<Blog>) JdbcUtil.executeQuery(sql, new QueryCallback() {
            @Override
            public Object doResultHandle(ResultSet resultSet) throws SQLException {
                List<Blog> list = new ArrayList<>();
                while (resultSet.next()) {
                    list.add(transformResult2Blog(resultSet));
                }
                return list;
            }
        });
    }

    /**
     * 查询最热微博
     *
     * @return
     */
    public List<Blog> selectHotBlog(Integer id, int count, int uid) throws SQLException {
        String sql = SELECT_BLOGS + uid;
        if (id != null) {
            sql += WHERE + id;
        }
        sql += " order by thumbs_up + comment desc limit " + count;
        System.out.println(sql);
        return (List<Blog>) JdbcUtil.executeQuery(sql, new QueryCallback() {
            @Override
            public Object doResultHandle(ResultSet resultSet) throws SQLException {
                List<Blog> list = new ArrayList<>();
                while (resultSet.next()) {
                    list.add(transformResult2Blog(resultSet));
                }
                return list;
            }
        });
    }

    /**
     * 查询我的微博
     *
     * @return
     */
    public List<Blog> selectMyBlog(Integer id, int count, int uid) throws SQLException {
        String sql = SELECT_BLOGS + uid;
        if (id != null) {
            sql += WHERE + id + " and blog.uid = " + uid;
        } else {
            sql += " where blog.uid = " + uid;
        }
        sql += " order by blog.id desc limit " + count;
        System.out.println(sql);
        return (List<Blog>) JdbcUtil.executeQuery(sql, new QueryCallback() {
            @Override
            public Object doResultHandle(ResultSet resultSet) throws SQLException {
                List<Blog> list = new ArrayList<>();
                while (resultSet.next()) {
                    list.add(transformResult2Blog(resultSet));
                }
                return list;
            }
        });
    }

    public void thumbsUp(int blogId) throws SQLException {
        String sql = "update blog set thumbs_up = thumbs_up + 1 where id = " + blogId;
        System.out.println(sql);
        JdbcUtil.execute(sql);
    }

    public void comment(int blogId) throws SQLException {
        String sql = "update blog set comment = comment + 1 where id = " + blogId;
        System.out.println(sql);
        JdbcUtil.execute(sql);
    }

    private Blog transformResult2Blog(ResultSet resultSet) throws SQLException {
        Blog blog = new Blog();
        User user = new User();

        blog.setId(resultSet.getInt(1));
        blog.setContent(resultSet.getString(3));
        blog.setThumbsUp(resultSet.getInt(4));
        blog.setComment(resultSet.getInt(5));
        blog.setCreateTime(resultSet.getDate(6));

        user.setId(resultSet.getInt(7));
        user.setImg(resultSet.getString(10));
        user.setNick(resultSet.getString(11));

        blog.setUser(user);

        blog.setThumbs(resultSet.getDate(17) != null);

        return blog;
    }
}
